package uit.nfcmenu.db.adapter;

import java.util.ArrayList;

import uit.nfcmenu.db.model.OrderModel;
import uit.nfcmenu.util.CommConstant;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.util.Log;


public class OrderAdapter extends DatabaseAdapter {
	String tag = OrderAdapter.class.getSimpleName();

	public OrderAdapter(Context context) {
		super(context);
	}

	private OrderModel createOrderModel(Cursor cursor) {
		OrderModel model = new OrderModel();
		try {
			model.setId(cursor.getInt(cursor.getColumnIndex(DbDefines.TableOrder.orderId)));
			model.setItemID(cursor.getInt(cursor.getColumnIndex(DbDefines.TableOrder.itemId)));
			model.setStatus(cursor.getString(cursor.getColumnIndex(DbDefines.TableOrder.status)).toUpperCase());
			model.setTableNo(cursor.getInt(cursor.getColumnIndex(DbDefines.TableOrder.tableNo)));
			model.setOrderTime(cursor.getString(cursor.getColumnIndex(DbDefines.TableOrder.orderTime)));
			model.setOrderUser(cursor.getString(cursor.getColumnIndex(DbDefines.TableOrder.orderUser)));
			model.setQuantity(cursor.getInt(cursor.getColumnIndex(DbDefines.TableOrder.quantity)));
			model.setDescription(cursor.getString(cursor.getColumnIndex(DbDefines.TableOrder.description)));
			model.setComment(cursor.getString(cursor.getColumnIndex(DbDefines.TableOrder.comment)));
			model.setmDate(cursor.getLong(cursor.getColumnIndex(DbDefines.TableOrder.orderTimeMilisecond)));
		} catch (CursorIndexOutOfBoundsException e) {
			Log.i(tag, "cursor error "+e);
		}
		return model;
	}

	private ContentValues createContentValues(OrderModel model) {
		ContentValues contentValues = new ContentValues();

		contentValues.put(DbDefines.TableOrder.orderId, model.getId());
		contentValues.put(DbDefines.TableOrder.itemId, model.getItemID());
		contentValues.put(DbDefines.TableOrder.status, model.getStatus());
		contentValues.put(DbDefines.TableOrder.tableNo, model.getTableNo());
		contentValues.put(DbDefines.TableOrder.orderTime, model.getOrderTime());
		contentValues.put(DbDefines.TableOrder.orderUser, model.getOrderUser());
		contentValues.put(DbDefines.TableOrder.quantity, model.getQuantity());
		contentValues.put(DbDefines.TableOrder.description, model.getDescription());
		contentValues.put(DbDefines.TableOrder.comment, model.getComment());
		contentValues.put(DbDefines.TableOrder.orderTimeMilisecond, model.getmDate());
		return contentValues;
	}
	
	public int insertOrderModel(OrderModel model) {
		Log.i(tag, "insertOrderModel() model: " + model.getId());
		int value = -1;

		this.open();

		try {
			ContentValues contentValues = createContentValues(model);

			value = (int) this.mDb.insert(DbDefines.OrderModel, "", contentValues);

		} finally {
			this.close();
		}

		Log.w(tag, "insertOrderModel() value=" + value);
		return value;
	}

	public int deleteOrderModel(int orderID) {
		int result = 0;
		this.open();
		try {
			result = this.mDb.delete(DbDefines.OrderModel,
					DbDefines.TableOrder.orderId + " = " + orderID, null);
		} finally {
			this.close();
		}
		return result;
	}

	public synchronized int updateOrderModel(OrderModel model) {
		Log.i(tag, "updateOrderModel " + model.toString());
		int value = -1;

		this.open();
		try {
			// Assign values to add...
			ContentValues contentValues = new ContentValues();
			contentValues.put(DbDefines.TableOrder.status, model.getStatus());
			contentValues.put(DbDefines.TableOrder.comment, model.getComment());

			// Add to database
			value = (int) this.mDb.update(DbDefines.OrderModel, contentValues,
					DbDefines.TableOrder.orderId + " = " + model.getId(), null);
			if (value == 0) {
				this.close();
			}
		} finally {
			this.close();
		}

		Log.w(tag, "updateOrderModel() value=" + value);
		return value;
	}

	public OrderModel loadOrderModel(int orderID) {
		this.open();
		Cursor cursor = null;
		OrderModel model = null;
		try {
			cursor = this.mDb.query(DbDefines.OrderModel, new String[] {
					DbDefines.TableOrder.orderId, DbDefines.TableOrder.itemId,
					DbDefines.TableOrder.status, DbDefines.TableOrder.tableNo,
					DbDefines.TableOrder.orderTime, DbDefines.TableOrder.quantity,
					DbDefines.TableOrder.description, DbDefines.TableOrder.comment,
					DbDefines.TableOrder.orderUser,DbDefines.TableOrder.orderTimeMilisecond },
					DbDefines.TableOrder.orderId + " = " + orderID ,
					null, "", "", "", "1");

			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				model = createOrderModel(cursor);
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return model;
	}

	public ArrayList<OrderModel> getOrdersInDB() {
		long temp=System.currentTimeMillis()-CommConstant.TWO_DATE;
		Log.w(tag, "getOrdersInDB()");
		this.open();
		Cursor cursor = null;
		OrderModel model = null;
		ArrayList<OrderModel> models = new ArrayList<OrderModel>();
		try {
			cursor = this.mDb.query(DbDefines.OrderModel, new String[] {
					DbDefines.TableOrder.orderId, DbDefines.TableOrder.itemId,
					DbDefines.TableOrder.status, DbDefines.TableOrder.tableNo,
					DbDefines.TableOrder.orderTime, DbDefines.TableOrder.quantity,
					DbDefines.TableOrder.description, DbDefines.TableOrder.comment,
					DbDefines.TableOrder.orderUser ,DbDefines.TableOrder.orderTimeMilisecond}, 
					DbDefines.TableOrder.orderTimeMilisecond +">" + temp , null, "", "", "");
			if (cursor != null) {
				while (cursor.moveToNext()) {
					model = createOrderModel(cursor);
					models.add(model);
					Log.i(tag, "get model: "+model.toString());
				}
			}

		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return models;
	}
//	public ArrayList<OrderModel> getOrdersInDB(String date) {
//		Log.w(tag, "getOrdersInDB() date="+date);
//		this.open();
//		Cursor cursor = null;
//		OrderModel model = null;
//		ArrayList<OrderModel> models = new ArrayList<OrderModel>();
//		
//		String where = DbDefines.TableOrder.orderTime+" like "+"'"+date+"%"+"'";
//		try {
//			cursor = this.mDb.query(DbDefines.OrderModel, new String[] {
//					DbDefines.TableOrder.orderId, DbDefines.TableOrder.itemId,
//					DbDefines.TableOrder.status, DbDefines.TableOrder.tableNo,
//					DbDefines.TableOrder.orderTime, DbDefines.TableOrder.quantity,
//					DbDefines.TableOrder.description, DbDefines.TableOrder.comment,
//					DbDefines.TableOrder.orderUser,DbDefines.TableOrder.orderTimeMilisecond }, 
//					where, null, "", "", "");
//			if (cursor != null) {
//				while (cursor.moveToNext()) {
//					model = createOrderModel(cursor);
//					models.add(model);
//					Log.i(tag, "get model: "+model.toString());
//				}
//			}
//
//		} finally {
//			if (cursor != null) {
//				cursor.close();
//			}
//			this.close();
//		}
//		return models;
//	}

	/**
	 * Delete database
	 */
	public void deleteAllOrders() {
		//Log.i(tag, "deleteAllOrders()");
		this.open();
		mDb.delete(DbDefines.OrderModel, null, null);
		this.close();
	}

	public int deleteDummyData() {
		long temp=System.currentTimeMillis()-CommConstant.TWO_DATE;
		int result = 0;
		this.open();
		try {
			result = this.mDb.delete(DbDefines.OrderModel,
					DbDefines.TableOrder.orderTimeMilisecond +"<" + temp, null);
		} finally {
			this.close();
		}
		return result;
		
	}
}
